package com.sj.purchase.modules.cost.web;

import com.sj.purchase.common.utils.mapper.BeanMapper;
import com.sj.purchase.modules.cost.domain.price.CostPrice;
import com.sj.purchase.modules.cost.repository.price.CostPriceRepository;
import com.sj.purchase.modules.cost.views.price.CostPriceVo;
import com.sj.purchase.util.ExcelDownloadUtil;
import com.sj.purchase.util.OrderTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Alin on 2017/11/13.
 */
@RestController
@RequestMapping("api/cost/price")
public class CostPriceController {
    @Autowired
    private CostPriceRepository costPriceRepository;

    @GetMapping("/list")
    private ResponseEntity getList(String sku, Date startDate, Date endDate, Pageable pageable){
        try {
            return ResponseEntity.ok(getListVo(sku,startDate,endDate,pageable));
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.ok("");
    }

    @GetMapping("/excel")
    private void excel(String sku, Date startDate, Date endDate){
        ExcelDownloadUtil.easyPoi("移动平均成本价", CostPriceVo.class,getListVo(sku,startDate,endDate,null).getContent().stream().collect(Collectors.toList()));
    }


    /**
     * 枚举与String转换
     * @param costPrice
     * @return
     */
    private CostPriceVo convert(CostPrice costPrice){
        CostPriceVo costPriceVo = BeanMapper.map(costPrice,CostPriceVo.class);
        costPriceVo.setOrderType(costPrice.getOrderType()!=null? OrderTypeUtils.getOrderType(costPrice.getOrderType()):"");
        return costPriceVo;
    }

    /**
     * 获取VoList
     * @param sku
     * @param startDate
     * @param endDate
     * @param pageable
     * @return
     */
    public Page<CostPriceVo> getListVo(String sku, Date startDate, Date endDate, Pageable pageable){
        Page<CostPrice> costPriceList = null;
        if(sku!=null&&!sku.isEmpty()&&startDate!=null&&endDate!=null){
            costPriceList = costPriceRepository.findBySkuNoAndCreateDateBetweenOrderByCreateDateDesc(sku,startDate,endDate,pageable);
        }else if(sku!=null&&!sku.isEmpty()){
            costPriceList = costPriceRepository.findBySkuNoOrderByCreateDateDesc(sku,pageable);
        }else if(startDate!=null&&endDate!=null){
            costPriceList = costPriceRepository.findByCreateDateBetweenOrderByCreateDateDesc(startDate,endDate,pageable);
        }else {
            costPriceList = costPriceRepository.findAll(pageable);
        }
        List<CostPriceVo> costPriceVoList = new ArrayList<>();
        if(costPriceList.hasContent()){
            costPriceVoList = costPriceList.getContent().stream().map(this::convert).collect(Collectors.toList());
        }
        return new PageImpl<>(costPriceVoList,pageable,costPriceList.getTotalElements());
    }




}
